import utils from "./utils.js"
import config from "./config.js"
let request = function(requestConfig){
	const token = config.token || ""
	const lesseeid = config.lesseeid || ""
	if(!requestConfig.header){
		requestConfig.header = {}
	}
	if(token){
		requestConfig.header['Authorization'] = 'Bearer ' + token
	}
	if(lesseeid){
		requestConfig.header['lesseeid'] = lesseeid
	}
	console.log(requestConfig);
	if (requestConfig.method === 'post' || requestConfig.method === 'put') {
		requestConfig.header['Content-Type'] = 'application/json'
	}
	let fail = function(res){
		let pages = getCurrentPages()
		let curPage = pages[pages.length-1]
		if(res.statusCode != 200){
			if(curPage && curPage.$refs.uToast){
				curPage.$refs.uToast.show({
					title: res.errMsg,
					type: 'error'
				})
			}
		}
	}
	if(!requestConfig.fail){
		requestConfig.fail = fail
	}
	return uni.request(requestConfig)
} 

export default request